/*
 *    Copyright (c) 2018-2025, proj All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the proj developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: proj
 */

package com.ykht.proj.coupon.controller.b;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ykht.proj.common.core.util.R;
import com.ykht.proj.common.log.annotation.SysLog;
import com.ykht.proj.common.security.util.SecurityUtils;
import com.ykht.proj.coupon.entity.*;
import com.ykht.proj.coupon.service.CnCouponLogService;
import com.ykht.proj.coupon.service.FxConfBrandPersonService;
import com.ykht.proj.coupon.service.FxRewardPersonService;
import com.ykht.proj.coupon.vo.CnCouponLogVo;
import com.ykht.proj.coupon.vo.FXRewardPersonLogVo;
import com.ykht.proj.coupon.vo.FxConfBrandPersonVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.time.LocalDateTime;


/**
 * 品牌对c端的分销表
 *
 * @author proj code generator
 * @date 2021-08-12 14:10:38
 */
@RestController
@AllArgsConstructor
@RequestMapping("/b/fxconfbrandperson")
@Api(value = "fxconfbrandperson", tags = "b端的分销表管理")
@Slf4j
public class BFxConfBrandPersonController {

	private final FxConfBrandPersonService fxConfBrandPersonService;
	private final FxRewardPersonService fxRewardPersonService;
	private final CnCouponLogService cnCouponLogService;

	/**
	 * 分页查询 品牌对c端的分销表
	 *
	 * @param page                分页对象
	 * @param fxConfBrandPersonVo 品牌对c端的分销表
	 * @return
	 */
	@ApiOperation(value = "分页查询 b端的分销表", notes = "分页查询 品牌对b端的分销表")
	@GetMapping("/page")
	public R<IPage<FxConfBrandPersonVo>> getFxConfBrandPersonPage(Page page, FxConfBrandPersonVo fxConfBrandPersonVo) {
		log.info("->分页查询 品牌对b端的分销表 :{}", page, fxConfBrandPersonVo);
		IPage<FxConfBrandPersonVo> iPage = fxConfBrandPersonService.getFxConfBrandPersonPage(page, fxConfBrandPersonVo);
		return R.ok(iPage);
	}


	/**
	 * 通过id查询品牌对c端的分销表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id查询", notes = "通过id查询")
	@GetMapping("/{id}")
	public R<FxConfBrandPersonVo> getById(@PathVariable("id") Long id) {
		log.info("->通过id查询品牌对b端的分销表 :{}", id);
		FxConfBrandPersonVo fxConfBrandPersonVo = fxConfBrandPersonService.getByIdFxConfBrandPerson(id);
		return R.ok(fxConfBrandPersonVo);
	}

	@ApiOperation(value = "新增或者新增 品牌对c端的分销表", notes = "新增或者新增 品牌对c端的分销表")
	@PostMapping("create")
	public R create(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
		log.info("->新增 品牌对c端的分销表 :{}", fxConfBrandPerson);
		return R.ok(fxConfBrandPersonService.create(fxConfBrandPerson));
	}

	@ApiOperation(value = "更新品牌对c端的分销表")
	@PostMapping("update")
	public R update(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
		log.info("->更新品牌对c端的分销表 :{}", fxConfBrandPerson);
		if(fxConfBrandPerson.getId()==null) return R.failed("id为空");
		return R.ok(fxConfBrandPersonService.updateObj(fxConfBrandPerson));
	}

	@ApiOperation(value = "结束品牌对c端的分销表")
	@PostMapping("finish")
	public R finish(@Validated @NotNull Long id) {
		log.info("->结束品牌对分销配置 :{}", id);
		if(id == null) return R.failed("id为空");
		return R.ok(fxConfBrandPersonService.finish(id));
	}
	/**
	 * 新增品牌对c端的分销表
	 *
	 * @param fxConfBrandPerson 品牌对c端的分销表
	 * @return R
	 */
	@ApiOperation(value = "新增品牌对b端的分销表", notes = "新增品牌对b端的分销表")
	@SysLog("新增品牌对b端的分销表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxconfbrandperson_add')")
	public R save(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
		return R.ok(fxConfBrandPersonService.save(fxConfBrandPerson));
	}

	/**
	 * 修改品牌对c端的分销表
	 *
	 * @param fxConfBrandPerson 品牌对c端的分销表
	 * @return R
	 */
	@ApiOperation(value = "修改品牌对b端的分销表", notes = "修改品牌对b端的分销表")
	@SysLog("修改品牌对b端的分销表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('coupon_fxconfbrandperson_edit')")
	public R updateById(@RequestBody FxConfBrandPerson fxConfBrandPerson) {
		return R.ok(fxConfBrandPersonService.updateById(fxConfBrandPerson));
	}

	/**
	 * 通过id删除品牌对c端的分销表
	 *
	 * @param id id
	 * @return R
	 */
	@ApiOperation(value = "通过id删除品牌对b端的分销表", notes = "通过id删除品牌对b端的分销表")
	@SysLog("通过id删除品牌对b端的分销表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('coupon_fxconfbrandperson_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(fxConfBrandPersonService.removeById(id));
	}

	@ApiOperation(value = "通过id审核反馈", notes = "通过id审核反馈")
	@PostMapping("UpdateStatus")
	public R updateStatus(CnCouponLog cnCouponLog) {
		Integer status = cnCouponLog.getStatus();
		if (1 == status && status != null) {
			return R.ok(cnCouponLogService.updateById(cnCouponLog));
		}
		Long sjId = SecurityUtils.getSjId();
		int shopType = 2;
		fxRewardPersonService.updateReward(shopType, sjId);
		FXRewardPersonLogVo fxRewardPersonLogVo = fxRewardPersonService.selectMoeny();
		fxRewardPersonService.insert(fxRewardPersonLogVo);
		return R.ok();
	}


}
